package com.hxalex.simulationhandle.accessbilityService.receiver.search;

import android.util.Log;
import android.view.accessibility.AccessibilityNodeInfo;

import com.hxalex.simulationhandle.accessbilityService.manger.SearchManger;
import com.hxalex.simulationhandle.accessbilityService.receiver.base.BaseReceiver;
import com.wenming.library.NotifyUtil;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by hx_Alex on 2018/5/31.
 */

public class SearchWebContentCountReceiver extends BaseReceiver {
    private AccessibilityNodeInfo nodeInfo;
    private long waitCount=0;
    private static final long MAXCOUNT=2; 
    private waitCountThread waitCountThread=new waitCountThread();
    private boolean isThreadStart=false;
    private boolean isFor=true;
    public AccessibilityNodeInfo getNodeInfo() {
        return nodeInfo;
    }
    private NotifyUtil currentNotify;
    public void setNodeInfo(AccessibilityNodeInfo nodeInfo) {
        this.nodeInfo = nodeInfo;
    }

    public SearchWebContentCountReceiver(SearchManger searchManger) {
        super(searchManger);
        FLAG="搜索页面内容匹配总数命令";
    }


    @Override
    public void doSomething() {
        if(!isPrintCommandMsg)
        {
            Log.w("SearchWebContentCount","开始执行"+FLAG);
            isPrintCommandMsg=true;
        }
        if(nodeInfo==null) return;
        waitCount=0;
        searchManger.recycleSearchWebSiteInKeyWordReslutCount(nodeInfo);
     
        if(!isThreadStart)
        {
            waitCountThread.start();
            isThreadStart=true;
        }
         
 
        
    }
    
    
    class waitCountThread extends Thread
    {
        
        @Override
        public void run() {
            super.run();
            while (isFor){


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if(waitCount<MAXCOUNT)
                {
                    waitCount++;
                }else
                {
                    Log.i("SearchWebContentCount","匹配项检索完毕");
                    for(AccessibilityNodeInfo info:searchManger.getContentMatchList())
                    {
                        String text="";
                        if(info.getText()!=null)
                        {
                            text=info.getText().toString();
                        }else if(info.getContentDescription()!=null)
                        {
                            text=info.getContentDescription().toString();
                        }
                        Log.i("SearchWebContentCount","匹配项:"+text);
                    }
                    EventBus.getDefault().post(SearchManger.OPEN_WAKE_WINWODS);
                    isFor=false;
                    commandCompleteListener.commandCompleteListener();
                }
                    
          
            
            }
        }
    }
}
